package sctu.edu.java.year2016.team04.yanghuilin.day20160605.chapter11_3;

import java.util.Scanner;

/**
 * Created by dell on 2016/6/5.
 * 创建Computer类，该类中有一个计算两个数的最大公约数的方法，如果向该方法传递负整数，该方法就会抛出自定义异常。
 */
public class Computer {
    public void TheMaxCommonDivisor(int x, int y) throws MyException {
        if (x <=0 || y <= 0) {
            throw new MyException(x, y);
        } else {
            int z;
            int i;
            if (x >= y) {
                x = y;
                for (i = 1; i <= x; i++) {
                    if (x % i == 0 && y % i == 0) {
                        z = i;
                       System.out.println("The max common divisor is " + z);
                    }
                }

            } else {
                for (i = 1; i <= x; i++) {
                    if (x % i == 0 && y % i == 0) {
                        z= i;
                        System.out.println("The max common divisor is " + z);
                    }
                }
            }

        }
    }



    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("please enter two numbers.");

        int x=scanner.nextInt();
        int y=scanner.nextInt();
        Computer computer=new Computer();

        try {
            computer.TheMaxCommonDivisor(x,y);

        } catch (Exception ex) {
            System.out.println("Error"+ex.toString());
        }
    }
}

class MyException extends Exception {
    String message;

    public MyException(int x, int y) {
        message = "输入了非正整数";
        System.out.println(message);
    }
}

